import Vue from 'vue';
import {
    api,
} from '@/lib/api';
import {
    url,
} from '@/lib/utils';

import { Module } from 'vuex';
import { RootState } from '@/types/store';
import { stat } from 'fs';

export interface UserState {
    // 控制提示关注公众号
    ishowpopupTipsBox: any;
    // 用户基本信息
    basicInfo: User.Info | null;
    /** 代金券列表 */
    couponList: {
        [status: string]: ListWrap<number> & ListReq;
    };
    /** 代金券列表 */
    goodsCouponList: {
        [type_id_shopId: string]: User.Coupon[];
    };
    /** 分享代金券列表 */
    giftCouponList: ListWrap<User.Coupon> & ListReq;
    /** 代金券数据 */
    couponMap: {
        [couponId: number]: User.UsableCoupon;
    };
    referrer: User.Referrer | null;
    referrerList: {
        [name: string]: User.ReferrerListItem[];
    };
    /** 账户信息 */
    account: any;
    /** 平台最强 */
    accountRank: any;
    /** 未解锁金额 */
    unlock: any;
    /** 解锁金额 */
    lock: any;
    /** 邀请统计 */
    invitationStat: User.InvitationStatistics | null;
    /** 银行账号 */
    bankAccount: User.BankAccount[] | null;
    visitorList: ListWrap<User.VisitorRank> & ListReq;
    shareList: ListWrap<User.UserShare> & ListReq;
    /** 解锁排行榜 */
    unlockList: ListWrap<User.UnlockRankItem> & ListReq;
    /** 解锁排行榜-搜索门店 */
    unlockSearchNameList: ListWrap<User.unlockSeachNameLst> & ListReq;

    rebateDetailListMap: {
        [startDate_endDate_status_isUnlock: string]: ListWrap<Rebate.Detail> & ListReq;
    };
    unlockDetailListMap: any;
    /** 我的邀请列表 */
    userInvitedList: {
        [status: string]: ListWrap<User.UserInvited> & ListReq;
    };
    application: User.PartnerApplication | null;
    /** 解锁商品列表 */
    freeList: ListWrap<any> & ListReq;
    /** 废弃 */
    giftCouponInfo: any;
    withdrawList: ListWrap<User.WithdrawDetail> & ListReq;
    /** 返利会员店铺 */
    memberShopList: User.Member[];
    rankingList: string[];
    bindReferrerId: any;
    /** 提现相关订单 */
    withdrawOrder: {
        [withdrawDetailId: number]: Order.ListItem[];
    };
    exclusiveService: {
        name: string;
        avatar: string;
        mobile: string;
    } | null;
    // 我的账户搜索好友
    searchFriend: [];
    shopBnlance: [];
}

const m: Module<UserState, RootState> = {
    namespaced: true,
    state: {
        ishowpopupTipsBox: null,
        basicInfo: null,
        couponList: {},
        goodsCouponList: {},
        giftCouponList: null,
        couponMap: {},
        referrer: null,
        referrerList: {},
        account: null,
        accountRank: null,
        unlock: null,
        lock: null,
        invitationStat: null,
        bankAccount: null,
        visitorList: {
            list: [],
            loaded: false,
            req: false,
        },
        shareList: {
            list: [],
            loaded: false,
            req: false,
        },
        unlockList: {
            list: [],
            loaded: false,
            req: false,
        },
        // unlockSearchNameList: null,
        unlockSearchNameList: {
            list: [],
            loaded: false,
            req: false,
        },

        rebateDetailListMap: {},
        unlockDetailListMap: {},
        userInvitedList: {},
        application: null,
        freeList: {
            list: [],
            loaded: false,
            req: false,
        },
        giftCouponInfo: null,
        withdrawList: {
            list: [],
            loaded: false,
            req: false,
        },
        memberShopList: [],
        rankingList: [],
        bindReferrerId: null,
        withdrawOrder: {},
        exclusiveService: null,
        // 我的账户搜索好友
        searchFriend: [],
        shopBnlance: [],
    },
    mutations: {
        /**
         * 设置用户基本信息
         */
        setBasicInfo(state, data) {
            state.basicInfo = data;
        },
        /**
         * 绑定邀请人
         */
        bindReferrer(state, { referrerId }) {
            state.bindReferrerId = referrerId;
        },
        /**
         * 初始化
         */
        initCouponList(state, status) {
            Vue.set(state.couponList, status, {
                list: [],
                loaded: false,
                req: false,
            });
        },
        concatCouponList(state, { status, list }) {
            const ids = [];
            for (const coupon of list) {
                ids.push(coupon.id);
                Vue.set(state.couponMap, coupon.id, coupon);
            }
            state.couponList[status].list.push(...ids);
        },
        setCouponLoaded(state, { status, loaded }) {
            state.couponList[status].loaded = loaded;
        },
        setCouponReq(state, { status, req }) {
            state.couponList[status].req = req;
        },
        /**
         * 记录商品优惠信息
         */
        setGoodsCouponList(state, { key, list }) {
            Vue.set(state.goodsCouponList, key, list);
        },
        /**
         * 初始化
         */
        initGiftCouponList(state) {
            state.giftCouponList = {
                list: [],
                loaded: false,
                req: false,
            };
        },
        concatGiftCouponList(state, { list }) {
            state.giftCouponList.list.push(...list);
        },
        setGiftCouponLoaded(state, { loaded }) {
            state.giftCouponList.loaded = loaded;
        },
        setGiftCouponReq(state, { status, req }) {
            state.giftCouponList.req = req;
        },
        /**
         * 删除coupon
         */
        removeCouponFrom(state, { couponId, status }) {
            const wrap = state.couponList[status];
            if (wrap) {
                const pos = wrap.list.indexOf(couponId);
                if (pos >= 0) {
                    wrap.list.splice(pos, 1);
                }
            }
        },
        /** 记录推荐人信息列表 */
        setReferrerList(state, { name, list }) {
            Vue.set(state.referrerList, name, list);
        },
        /** 记录推荐人信息 */
        setReferrer(state, info) {
            state.referrer = info;
        },
        /**
         * 记录账户信息
         */
        setAccount(state, account) {
            state.account = account;
        },
        /**
         * 记录账户排行
         */
        setAccountRank(state, account) {
            state.accountRank = account;
        },
        /**
         * 记录邀请统计数据
         */
        setInvitationStat(state, stat) {
            state.invitationStat = stat;
        },
        /**
         * 记录银行账号
         */
        setBankAccount(state, bankAccount) {
            state.bankAccount = bankAccount;
        },
        /** 设置未解锁金额 */
        setUnlock(state, unlock) {
            state.unlock = unlock;
        },
        /** 设置解锁金额 */
        setLock(state, lock) {
            state.lock = lock;
        },
        /**
         * 初始化邀请列表
         */
        initVisitorList(state) {
            state.visitorList = {
                list: [],
                loaded: false,
                req: false,
            };
        },
        /**
         * 记录邀请排行列表
         */
        concatVisitorList(state, { list }) {
            state.visitorList.list.push(...list);
        },
        /**
         * 邀请排行列表是否全部加载
         */
        setVisitorListLoaded(state, { loaded }) {
            state.visitorList.loaded = loaded;
        },
        /**
         * 邀请排行列表是否全部加载
         */
        setVisitorListReq(state, { req }) {
            state.visitorList.req = req;
        },
        /**
         * 初始化分享列表
         */
        initShareList(state) {
            state.shareList = {
                list: [],
                loaded: false,
                req: false,
            };
        },
        /**
         * 记录分享排行列表
         */
        concatShareList(state, { list }) {
            state.shareList.list.push(...list);
        },
        /**
         * 分享列表是否全部加载
         */
        setShareListLoaded(state, { loaded }) {
            state.shareList.loaded = loaded;
        },
        /**
         * 分享列表是否全部加载
         */
        setShareListReq(state, { req }) {
            state.shareList.req = req;
        },
        /**
         * 初始化解锁排行列表
         */
        initUnlockList(state) {
            state.unlockList.list = [];
            state.unlockList.loaded = false;
            state.unlockList.req = false;
        },
        concatUnlockList(state, { list }) {
            state.unlockList.list.push(...list);
        },
        setUnlockListLoaded(state, { loaded }) {
            state.unlockList.loaded = loaded;
        },
        setUnlockListReq(state, { req }) {
            state.unlockList.req = req;
        },

        // 解锁排行-搜索门店
        // unlockSearchNameList(state, data){
        //     state.unlockSearchNameList = data;
        // }
        /**
         * 初始化搜索门店列表
         */
        initUnlockSearchNameList(state) {
            state.unlockSearchNameList.list = [];
            state.unlockSearchNameList.loaded = false;
            state.unlockSearchNameList.req = false;
        },
        /**
         * 搜索门店列表是否在请求
         */
        setUnlockSearchNameListReq(state, { req }) {
            state.unlockSearchNameList.req = req;
        },
        /**
         * 记录门店列表
         */
        concatUnlockSearchNameList(state, { list }) {
            state.unlockSearchNameList.list.push(...list);
        },
        /**
         * 门店列表是否全部加载
         */
        setUnlockSearchNameListLoaded(state, { loaded }) {
            state.unlockSearchNameList.loaded = loaded;
        },



        /**
         * 初始化返利列表
         */
        initRebateDetailList(state, key) {
            Vue.set(state.rebateDetailListMap, key, {
                list: [],
                loaded: false,
                req: false,
            });
        },
        /**
         * 记录化返利列表
         */
        concatRebateDetailList(state, { key, list }) {
            state.rebateDetailListMap[key].list.push(...list);
        },
        /**
         * 化返利列表是否全部加载
         */
        setRebateDetailListLoaded(state, { key, loaded }) {
            state.rebateDetailListMap[key].loaded = loaded;
        },
        /**
         * 化返利列表是否全部加载
         */
        setRebateDetailListReq(state, { key, req }) {
            state.rebateDetailListMap[key].req = req;
        },
        /**
         * 初始化返利列表
         */
        initUnlockDetailList(state, key) {
            Vue.set(state.unlockDetailListMap, key, {
                list: [],
                loaded: false,
                req: false,
            });
        },
        /**
         * 记录化返利列表
         */
        concatUnlockDetailList(state, { key, list }) {
            state.unlockDetailListMap[key].list.push(...list);
        },
        /**
         * 化返利列表是否全部加载
         */
        setUnlockDetailListLoaded(state, { key, loaded }) {
            state.unlockDetailListMap[key].loaded = loaded;
        },
        /**
         * 化返利列表是否全部加载
         */
        setUnlockDetailListReq(state, { key, req }) {
            state.unlockDetailListMap[key].req = req;
        },
        /**
         * 初始化我的邀请列表
         */
        initUserInvitedList(state, status) {
            Vue.set(state.userInvitedList, status, {
                list: [],
                loaded: false,
                req: false,
            });
        },
        concatUserInvitedList(state, { status, list }) {
            state.userInvitedList[status].list.push(...list);
        },
        setUserInvitedListLoaded(state, { status, loaded }) {
            state.userInvitedList[status].loaded = loaded;
        },
        setUserInvitedListReq(state, { status, req }) {
            state.userInvitedList[status].req = req;
        },
        setApplication(state, detail) {
            state.application = detail;
        },
        /**
         * 初始化免费商品列表
         */
        initFreeList(state) {
            state.freeList = {
                list: [],
                loaded: false,
                req: false,
            };
        },
        /**
         * 记录列表
         */
        concatFreeList(state, { list }) {
            state.freeList.list.push(...list);
        },
        /**
         * 列表是否全部加载
         */
        setFreeListLoaded(state, { loaded }) {
            state.freeList.loaded = loaded;
        },
        /**
         * 列表是否请求
         */
        setFreeListReq(state, { req }) {
            state.freeList.req = req;
        },
        /**
         * 初始化提现列表
         */
        initWithdrawList(state) {
            state.withdrawList = {
                list: [],
                loaded: false,
                req: false,
            };
        },
        /** 记录提现列表 */
        concatWithdrawList(state, list) {
            state.withdrawList.list = state.withdrawList.list.concat(list);
        },
        /** 记录提现列表加载完成 */
        setWithdrawListLoaded(state, loaded) {
            state.withdrawList.loaded = loaded;
        },
        /** 记录提现列表正在请求 */
        setWithdrawListReq(state, req) {
            state.withdrawList.req = req;
        },
        /** 记录返利会员店铺信息 */
        setMemberShopList(state, list) {
            state.memberShopList = list;
        },
        setRankingList(state, { data }) {
            state.rankingList = data;
        },
        /**
         * 获取提现相关的订单
         */
        setWithdrawOrder(state, { withdrawDetailId, list }) {
            Vue.set(state.withdrawOrder, withdrawDetailId, list);
        },
        /**
         * 记录专属客服
         */
        setExclusiveService(state, data) {
            state.exclusiveService = data;
        },
        // 我的账户搜索好友
        setSearchFriend(state, data){
            state.searchFriend = data;
        },
        setShopBanlance(state, data){
            state.shopBnlance = data;
        }
    },
    actions: {
        /**
         *  新增  更新  个人基本信息
         */
        async UpdateUserInfo({ state, commit }, { avatar, nickname, forceUpdate }) {
            if (state.basicInfo && !forceUpdate) {
                return;
            }
            const res = await api.post({
                url: `${url}/api/user/update_info`,
                data: {
                    avatar:avatar,
                    nickname:nickname
                },
            });
            const rdata = res.data;
            if (res.statusCode === 200 && rdata.status === 0) {
                state.basicInfo.avatar = avatar;
                state.basicInfo.nickname = nickname;
            }
            return res;
        },
        /**
         * 获取个人基本信息
         */
        async getBasicInfo({ state, commit }, forceUpdate) {
            if (state.basicInfo && !forceUpdate) {
                return;
            }
            const res = await api.get({
                url: `${url}/api/user/info`,
            });
            const rdata = res.data;
            if (res.statusCode === 200 && rdata.status === 0) {
                commit('setBasicInfo', rdata.data);
            }
            return res;
        },
        /**
         * 获取代金券列表
         */
        async getCouponList({ state, commit }, { status, offset, count, forceUpdate }) {
            if (!state.couponList[status] || forceUpdate) {
                commit('initCouponList', status);
            }
            const wrap = state.couponList[status];
            if (wrap.loaded || wrap.req) {
                return;
            }
            const end = offset + count;
            const length = wrap.list.length;
            if (end <= length) {
                return;
            }
            if (offset < length) {
                offset = length;
                count = end - offset;
            }
            commit('setCouponReq', { status, req: true });
            const res = await api.get({
                url: `${url}/api/user/usable_coupon`,
                data: {
                    status,
                    offset,
                    count,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                const list = res.data.data;
                commit('concatCouponList', {
                    status,
                    list,
                });
                if (list.length < count) {
                    commit('setCouponLoaded', { status, loaded: true });
                }
            }
            commit('setCouponReq', { status, req: false });
            return res;
        },
        /**
         * 获取代金券列表
         */
        async getGoodsCouponList({ state, commit }, { id, shopId, type, forceUpdate }) {
            const key = `${type}_${id}_${shopId}`;
            const res = await api.get({
                url: `${url}/api/user/goods_coupon`,
                data: {
                    id,
                    shop_id: shopId,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                const list = res.data.data;
                commit('setGoodsCouponList', {
                    key,
                    list,
                });
            }
            return res;
        },
        /**
         * 获取分享优惠券列表
         */
        async getGiftCouponList({ state, commit }, { offset, count, forceUpdate }) {
            if (!state.giftCouponList || forceUpdate) {
                commit('initGiftCouponList');
            }
            const wrap = state.giftCouponList;
            if (wrap.loaded || wrap.req) {
                return;
            }
            const end = offset + count;
            const length = wrap.list.length;
            if (end <= length) {
                return;
            }
            if (offset < length) {
                offset = length;
                count = end - offset;
            }
            commit('setGiftCouponReq', { req: true });
            const res = await api.get({
                url: `${url}/api/user/coupon`,
                data: {
                    offset,
                    count,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                const list = res.data.data;
                commit('concatGiftCouponList', {
                    list,
                });
                if (list.length < count) {
                    commit('setGiftCouponLoaded', { loaded: true });
                }
            }
            commit('setGiftCouponReq', { req: false });
            return res;
        },
        /** 获取推荐人列表 */
        async getReferrerList({ state, commit }, { name, shopId, forceUpdate }) {
            // if (!forceUpdate && state.referrerList[name] && state.referrerList[name].length) {
            //     return;
            // }
            if (!name) {
                return;
            }
            const res = await api.post({
                url: `${url}/api/user/referrer_list`,
                data: {
                    name: name || null,
                    shop_id: shopId || [],
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                commit('setReferrerList', {
                    name,
                    list: res.data.data,
                });
            }
            return res;
        },
        /** 获取推荐人信息 */
        async getReferrerInfo({ state, commit }, forceUpdate) {
            const res = await api.get({
                url: `${url}/api/user/referrer`,
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                commit('setReferrer', res.data.data);
            }
            return res;
        },
        /** 绑定邀请人 已废弃 */
        // async bindReferrer({ state, commit }, { referrerId }) {
        //     const res = await api.post({
        //         url: `${url}/api/user/bind_referrer`,
        //         data: {
        //             id: referrerId,
        //         },
        //     });
        //     return res;
        // },
        /** 记录访客 */
        async addReferrer({ state, commit }, { referrerId }) {
            const res = await api.post({
                url: `${url}/api/user/visitor`,
                data: {
                    referrer_id: referrerId,
                },
            });
            return res;
        },
        /**
         * 获得账户信息
         */
        async getAccount(context) {
            const res = await api.get({
                url: `${url}/api/user/account`,
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                context.commit('setAccount', res.data.data);
            }
            return res;
        },
        /**
         * 获得账户信息
         */
        async getAccountRank(context) {
            const res = await api.get({
                url: `${url}/api/user/account_rank`,
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                context.commit('setAccountRank', res.data.data);
            }
            return res;
        },
        /**
         * 获得账户流水
         */
        async getAccountDetail(context) {
            const res = await api.get({
                url: `${url}/api/user/account_detail`,
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                context.commit('setAccount', res.data.data);
            }
            return res;
        },
        /**
         * 获得邀请统计
         */
        async getInvitationStat(context) {
            const res = await api.get({
                url: `${url}/api/user/invitation_statistic`,
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                context.commit('setInvitationStat', res.data.data);
            }
            return res;
        },
        /**
         * 获取银行账户信息
         */
        async getBankAccount(context) {
            const res = await api.get({
                url: `${url}/api/user/bank`,
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                context.commit('setBankAccount', res.data.data);
            }
            return res;
        },
        /**
         * 更新银行账户信息
         */
        async updateBankAccount(context, { id, name, bank, no }) {
            const data: any = {
                name,
                bank_name: bank,
                no,
            };
            if (id) {
                data.id = id;
            }
            const response = await api.post({
                url: `${url}/api/user/bank`,
                data,
            });
            const rdata = response.data;
            if (rdata.status === 0) {
                await context.dispatch('getBankAccount');
            }
            return rdata;
        },
        /**
         * 删除银行账户信息
         */
        async deleteBankAccount(context, { id }) {
            const response = await api.delete({
                url: `${url}/api/user/bank`,
                data: {
                    id,
                },
            });
            const rdata = response.data;
            if (rdata.status === 0) {
                await context.dispatch('getBankAccount');
            }
            return rdata;
        },
        /**
         * 执行提现
         */
        async withdraw(context, { accountId }) {
            const res = await api.post({
                url: `${url}/api/user/apply/withdrawal`,
                data: {
                    id: accountId,
                },
            });
            return res;
        },
        /**
         * 添加邀请记录
         */
        async addInviteRecord(context, { mobile, name }) {
            const res = await api.get({
                url: `${url}/api/user/bind`,
                data: {
                    mobile,
                    name,
                },
            });
            return res;
        },
        /**
         * 检查邀请人
         */
        async checkReferrer(context, { shopId, userId }) {
            const res = await api.post({
                url: `${url}/api/user/check_referrer`,
                data: {
                    shop_id: shopId,
                    user_id: userId,
                },
            });
            const rdata = res.data;
            return rdata;
        },
        /**
         * 获得解锁账户信息
         */
        async getUnlock(context) {
            const res = await api.get({
                url: `${url}/api/user/unlock`,
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                context.commit('setUnlock', res.data.data.unlock);
                context.commit('setLock', res.data.data.lock);
            }
            return res;
        },
        /**
         * 获取邀请排行列表
         */
        async getVisitorList({ state, commit }, { offset, count, forceUpdate }) {
            if (!state.visitorList || forceUpdate) {
                commit('initVisitorList');
            }
            const wrap = state.visitorList;
            if (wrap.loaded || wrap.req) {
                return;
            }
            const end = offset + count;
            const length = wrap.list.length;
            if (end <= length) {
                return;
            }
            if (offset < length) {
                offset = length;
                count = end - offset;
            }
            commit('setVisitorListReq', { req: true });
            const res = await api.get({
                url: `${url}/api/user/visitor/rank`,
                data: {
                    offset,
                    count,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                const list = res.data.data;
                commit('concatVisitorList', {
                    list,
                });
                if (list.length < count) {
                    commit('setVisitorListLoaded', { loaded: true });
                }
            }
            commit('setVisitorListReq', { req: false });
            return res;
        },
        /**
         * 获取解锁排行列表
         */
        async getUnlockList({ state, commit }, { interval, year, offset, count, forceUpdate }) {
            if (!state.unlockList || forceUpdate) {
                commit('initUnlockList');
            }
            const wrap = state.unlockList;
            if (wrap.loaded || wrap.req) {
                return;
            }
            const end = offset + count;
            const length = wrap.list.length;
            if (end <= length) {
                return;
            }
            if (offset < length) {
                offset = length;
                count = end - offset;
            }
            commit('setUnlockListReq', { req: true });
            const res = await api.get({
                url: `${url}/api/user/unlock/rank`,
                data: {
                    interval,
                    year,

                    offset,
                    count,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                const list = res.data.data;
                commit('concatUnlockList', {
                    list,
                });
                if (list.length < count) {
                    commit('setUnlockListLoaded', { loaded: true });
                }
            }
            commit('setUnlockListReq', { req: false });
            return res;
        },
        // 解锁排行-门店搜索
        async getunlockSearchNameList({ state, commit }, { searchName,interval , year, offset, count, forceUpdate }){
            if (!state.unlockSearchNameList || forceUpdate) {
                commit('initUnlockSearchNameList');
            }
            const wrap = state.unlockSearchNameList;
            if (wrap.loaded || wrap.req) {
                return;
            }
            const end = offset + count;
            const length = wrap.list.length;
            if (end <= length) {
                return;
            }
            if (offset < length) {
                offset = length;
                count = end - offset;
            }
            commit('setUnlockSearchNameListReq', { req: true });
            const res = await api.get({
                url: `${url}/api/user/unlock/rank_shop`, //rank
                data: {
                    searchName,
                    interval,
                    year,
                    offset,
                    count,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                const list = res.data.data;
                commit('concatUnlockSearchNameList', {
                    list,
                });
                if (list.length < count) {
                    commit('setUnlockSearchNameListLoaded', { loaded: true });
                }
            }
            commit('setUnlockSearchNameListReq', { req: false });
            return res;
        },
        /**
         * 获取分享排行列表
         */
        async getShareList({ state, commit }, { offset, count, forceUpdate }) {
            if (!state.shareList || forceUpdate) {
                commit('initShareList');
            }
            const wrap = state.shareList;
            if (wrap.loaded || wrap.req) {
                return;
            }
            const end = offset + count;
            const length = wrap.list.length;
            if (end <= length) {
                return;
            }
            if (offset < length) {
                offset = length;
                count = end - offset;
            }
            commit('setShareListReq', { req: true });
            const res = await api.get({
                url: `${url}/api/user/share`,
                data: {
                    offset,
                    count,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                const list = res.data.data;
                commit('concatShareList', {
                    list,
                });
                if (list.length < count) {
                    commit('setShareListLoaded', { loaded: true });
                }
            }
            commit('setShareListReq', { req: false });
            return res;
        },
        /**
         * 获取返利明细列表
         */
        async getRebateDetailList({ state, commit }, { offset, count, startDate, endDate, status, isUnlock, search, forceUpdate }) {
            const key = `${startDate}_${endDate}_${status}_${isUnlock}_${search}`;
            if (!state.rebateDetailListMap[key] || forceUpdate) {
                commit('initRebateDetailList', key);
            }
            const wrap = state.rebateDetailListMap[key];
            if (wrap.loaded || wrap.req) {
                return;
            }
            const end = offset + count;
            const length = wrap.list.length;
            if (end <= length) {
                return;
            }
            if (offset < length) {
                offset = length;
                count = end - offset;
            }
            commit('setRebateDetailListReq', { key, req: true });
            const data = {
                offset,
                count,
                search,
                is_unlock: isUnlock ? 1 : 0,
            };
            if (startDate && endDate && status) {
                Object.assign(data, {
                    // start: startDate,
                    // end: endDate,
                    status,
                });
            }
            const res = await api.get({
                url: `${url}/api/rebate/detail`,
                data,
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                const list = res.data.data;
                commit('concatRebateDetailList', {
                    key,
                    list,
                });
                if (list.length < count) {
                    commit('setRebateDetailListLoaded', { key, loaded: true });
                }
            }
            commit('setRebateDetailListReq', { key, req: false });
            return res;
        },
        /**
         * 获取解锁明细列表
         */
        async getUnlockDetailList({ state, commit }, { offset, count, startDate, endDate, status, forceUpdate }) {
            const key = `${startDate}_${endDate}_${status}`;
            if (!state.unlockDetailListMap[key] || forceUpdate) {
                commit('initUnlockDetailList', key);
            }
            const wrap = state.unlockDetailListMap[key];
            if (wrap.loaded || wrap.req) {
                return;
            }
            const end = offset + count;
            const length = wrap.list.length;
            if (end <= length) {
                return;
            }
            if (offset < length) {
                offset = length;
                count = end - offset;
            }
            commit('setUnlockDetailListReq', { key, req: true });
            const data = {
                offset,
                count,
            };
            if (startDate && endDate && status) {
                Object.assign(data, {
                    // start: startDate,
                    // end: endDate,
                    status,
                });
            }
            const res = await api.get({
                url: `${url}/api/free/unlock`,
                data,
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                const list = res.data.data;
                commit('concatUnlockDetailList', {
                    key,
                    list,
                });
                if (list.length < count) {
                    commit('setUnlockDetailListLoaded', { key, loaded: true });
                }
            }
            commit('setUnlockDetailListReq', { key, req: false });
            return res;
        },
        /**
         * 获取我的邀请列表
         */
        async getUserInvitedList({ state, commit }, { status, offset, count, forceUpdate }) {
            if (!state.userInvitedList[status] || forceUpdate) {
                commit('initUserInvitedList', status);
            }
            const wrap = state.userInvitedList[status];
            if (wrap.loaded || wrap.req) {
                return;
            }
            const end = offset + count;
            const length = wrap.list.length;
            if (end <= length) {
                return;
            }
            if (offset < length) {
                offset = length;
                count = end - offset;
            }
            commit('setUserInvitedListReq', { status, req: true });
            const res = await api.get({
                url: `${url}/api/user/invited`,
                data: {
                    has_order: status,
                    offset,
                    count,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                const list = res.data.data;
                commit('concatUserInvitedList', {
                    status,
                    list,
                });
                if (list.length < count) {
                    commit('setUserInvitedListLoaded', { status, loaded: true });
                }
            }
            commit('setUserInvitedListReq', { status, req: false });
            return res;
        },
        async getApplication({ state, commit }) {
            const res = await api.get({
                url: `${url}/api/user/partner/application`,
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                commit('setApplication', res.data.data);
            }
            return res;
        },
        /**
         * 获取解锁商品列表
         */
        async getFreeList({ state, commit }, { offset, count, forceUpdate }) {
            if (!state.freeList || forceUpdate) {
                commit('initFreeList');
            }
            const wrap = state.freeList;
            if (wrap.loaded || wrap.req) {
                return;
            }
            const end = offset + count;
            const length = wrap.list.length;
            if (end <= length) {
                return;
            }
            if (offset < length) {
                offset = length;
                count = end - offset;
            }
            commit('setFreeListReq', { req: true });
            const res = await api.get({
                url: `${url}/api/free/list`,
                data: {
                    offset,
                    count,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                const list = res.data.data;
                commit('concatFreeList', {
                    list,
                });
                if (list.length < count) {
                    commit('setFreeListLoaded', { loaded: true });
                }
            }
            commit('setFreeListReq', { req: false });
            return res;
        },
        async getGiftCouponCode({ state, commit }, { id }) {
            const res = await api.get({
                url: `${url}/api/user/give_coupon`,
                data: {
                    id,
                },
            });
            return res.data;
        },
        async takeGiftCoupon(context, { code }) {
            const res = await api.get({
                url: `${url}/api/user/take/coupon`,
                data: {
                    code,
                },
            });
            return res;
        },
        async getWithdrawDetail({ state, commit }, { offset, count, forceUpdate }) {
            if (offset === 0 && forceUpdate) {
                commit('initWithdrawList');
            }
            const wrap = state.withdrawList;
            if (wrap.loaded || wrap.req) {
                return;
            }
            const end = offset + count;
            const length = wrap.list.length;
            if (end <= length) {
                return;
            }
            if (offset < length) {
                offset = length;
                count = end - offset;
            }
            commit('setWithdrawListReq', true);
            const res = await api.get({
                url: `${url}/api/user/withdrawal_detail`,
                data: {
                    offset,
                    count,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                const list = res.data.data;
                commit('concatWithdrawList', list);
                if (list.length < count) {
                    commit('setWithdrawListLoaded', true);
                }
            }
            commit('setWithdrawListReq', false);
            return res;
        },
        /**
         * 获取注册成会员的店铺
         */
        async getMemberShopList({ state, commit }, forceUpdate) {
            if (state.memberShopList.length && !forceUpdate) {
                return;
            }
            const res = await api.get({
                url: `${url}/api/user/member`,
                data: {
                    offset: 0,
                    count: 1000,
                },
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                const list = res.data.data;
                commit('setMemberShopList', list);
            }
            return res;
        },
        async getRankingList(context, { type }) {
            let reqUrl = `${url}/api/team/`;
            if (type === 'partner') {
                reqUrl += 'partner_ranking_list';
            } else {
                reqUrl += 'ranking_list';
            }
            const res = await api.get({
                url: reqUrl,
            });
            if (res.statusCode === 200 && res.data.status === 0) {
                context.commit('setRankingList', {
                    data: res.data.data,
                });
            }
            return res;
        },
        async getMobileCode(context, { mobile }) {
            const res = await api.get({
                url: `${url}/api/user/mobile_code`,
                data: {
                    mobile,
                },
            });
            const rdata = res.data;
            return rdata;
        },
        async bindMobile(context, { serialNumber, code }) {
            const res = await api.get({
                url: `${url}/api/user/bind_mobile`,
                data: {
                    serial_number: serialNumber,
                    code,
                },
            });
            const rdata = res.data;
            return rdata;
        },
        /**
         * 获取提现相关的订单信息
         */
        async getWithdrawOrder(context, { withdrawDetailId }) {
            const response = await api.get({
                url: `${url}/api/user/withdrawal/order`,
                data: {
                    id: withdrawDetailId,
                },
            });
            const rdata = response.data;
            if (rdata.status === 0) {
                context.commit('setWithdrawOrder', {
                    withdrawDetailId,
                    list: rdata.data,
                });
            }
            return rdata;
        },
        /**
         * 专属客服
         */
         async getExclusiveService(context) {
            const res = await api.get({
                url: `${url}/api/user/exclusive_service`,
            });
            const rdata = res.data;
            if (rdata && rdata.status === 0) {
                context.commit('setExclusiveService', rdata.data);
            }
            return rdata;
        },
        // 我的账户搜索好友
        async getSearchFriend({state, commit}, { search, offset, count } ){
            const res= await api.get({
                url: `${url}/api/rebate/query_user`,
                data: {
                    search,
                    offset,
                    count
                }
            });
            const rdata = res.data;
            if(rdata && rdata.status === 0){
                commit('setSearchFriend',rdata.data)
            }
            return rdata;

        },
        // 我的账户转赠好友
        async postTransfer({state, commit}, { user_id, id } ){
            const res= await api.post({
                url: `${url}/api/rebate/transfer `,
                data: {
                    user_id,
                    id
                }
            });
            const rdata = res.data;
            if(rdata && rdata.status === 0){
                commit('setSearchFriend',rdata.data)
            }
            return rdata;
        },
        // 获取会员在各店铺充值余额列表
        async getShopBanlance({state, commit}, number ){
            const res= await api.get({
                url: `${url}/api/user/balance`,
                // data: {
                //     number
                // }
            });
            const rdata = res.data;
            if(rdata && rdata.status === 0){
                commit('setShopBanlance',rdata.data)
            }
            return rdata;
        },
    },
};

export default m;
